﻿using System;
using System.Activities;

namespace NMemcached.Client.Activities
{
    /// <summary>
    /// Provides an abstract base class for Memcached activities requiring a cache key
    /// </summary>
    public abstract class KeyedMemecachedActivity : MemcachedActivity
    {
        /// <summary>
        /// Gets or sets the cache key
        /// </summary>
        public InArgument<string> Key { get; set; }

        /// <summary>
        /// Gets or sets the cache key for use by derived classes
        /// </summary>
        protected string CacheKey { get; set; }

        /// <summary>
        /// Initializes a new instance of the KeyedMemecachedActivity class
        /// </summary>
        protected KeyedMemecachedActivity()
            : base()
        {
        }

        /// <summary>
        /// Performs the execution of the activity
        /// </summary>
        /// <param name="context">The execution context under which the activity executes</param>
        protected override void Execute(CodeActivityContext context)
        {
            base.Execute(context);
            string key = this.Key.Get(context);
            if (String.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException("Key");
            }
            else
            {
                this.CacheKey = this.Key.Get(context);
            }
        }
    }
}
